<?php if (!defined('THINK_PATH')) exit();?><!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <meta name="renderer" content="webkit">
    <title></title>
    <link rel="stylesheet" href="<?php echo (C("CSS_URL")); ?>/pintuer.css">
    <link rel="stylesheet" href="<?php echo (C("CSS_URL")); ?>/admin.css">
    <link rel="stylesheet" href="<?php echo (C("CSS_URL")); ?>/index.css">
    <script src="<?php echo (C("JS_URL")); ?>/jquery.js"></script>
    <script src="<?php echo (C("JS_URL")); ?>/pintuer.js"></script>
    <link href="/Public/css/noty/jquery.noty.css" rel="stylesheet">
    <script src="/Public/Js/noty/jquery.noty.js"></script>
    <link href="/Public/css/noty/noty_theme_default.css" rel="stylesheet">
</head>
<?php echo hook('showMsg');?>
<body>
<div class="panel admin-panel">
  <div class="panel-head" id="add"><strong><span class="icon-pencil-square-o"></span><?php echo ($title); ?></strong></div>
  <div class="body-content">
    <form class="form-x"  enctype="multipart/form-data" name="user">
      <div class="form-group">
        <div class="label">
          <label>用户名：</label>
        </div>
        <div class="field">
          <input type="text" class="input w50" value="" name="username"  placeholder="请输入用户名"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id="username" style="color:#F00;"></span>
          <div class="tips"></div>
        </div>
      </div>
        <div class="form-group">
            <div class="label">
                <label>密码：</label>
            </div>
            <div class="field">
                <input type="password" class="input w50" value="" name="password"  placeholder="请输入密码" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id="password" style="color:#F00;"></span>
                <div class="tips"></div>
            </div>
        </div>
        <div class="form-group">
            <div class="label">
                <label>确认密码：</label>
            </div>
            <div class="field">
                <input type="password" class="input w50" value="" name="repassword"  placeholder="请输入密码" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id="repassword" style="color:#F00;"></span>
                <div class="tips"></div>
            </div>
        </div>
        <div class="form-group">
            <div class="label">
                <label>昵称：</label>
            </div>
            <div class="field">
                <input type="text" class="input w50" value="" name="eName"  placeholder="请输入昵称"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id="eName" style="color:#F00;"></span>
                <div class="tips"></div>
            </div>
        </div>
        <div class="form-group">
            <div class="label">
                <label>分配角色：</label>
            </div>
            <div class="field">
                <div>
                <?php if(is_array($role)): foreach($role as $key=>$vo): if($key % 5 == 0): ?><br /><br /><?php endif; ?>
                    <input type="checkbox" name="role_id[]" value="<?php echo ($vo['id']); ?>"><?php echo ($vo['role_name']); ?>&nbsp;&nbsp;<?php endforeach; endif; ?></div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id="role_id" style="color:#F00; margin-left: 350px;"></span>
                <div class="tips"></div>
            </div>
        </div>
        <div class="form-group">
            <div class="label">
                <label>联系方式：</label>
            </div>
            <div class="field">
                <input type="text" class="input w50" value="" name="tel"   placeholder="请输入联系方式"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id="tel" style="color:#F00;"></span>
                <div class="tips"></div>
            </div>
        </div>
      <div class="clear"></div>
      <div class="form-group">
        <div class="label">
          <label></label>
        </div>
        <div class="field">
          <div class="button bg-main icon-check-square-o tijiao" style="cursor:pointer;"> 提交</div>
        </div>
      </div>
    </form>
  </div>
</div>

</body></html>
<script>
    //验证手机号的唯一性
    function checkTel(tel,obj){
        $.ajax({
            url:'/Admin/Admin/checkTelUnique',
            type:'post',
            data:{tel:tel,type:"1"},
            dataType:'json',
            success:function(re){
                obj.html(re.tips);
            }
        });
    }
   //验证用户唯一性
    function checkUser(username,obj){
        $.ajax({
            url:'/Admin/Admin/checkUserUnique',
            type:'post',
            data:{username:username,type:'1'},
            dataType:'json',
            success:function(re){
                obj.html(re.tips);
            }
        });
    }
    //验证用户名
    var attr_name=obj_tips='';
    $('input[name="username"]').blur(function(){
        attr_name=$(this).prop('name');
        obj_tips=$('#'+attr_name);
        if($(this).val()==''){
            obj_tips.html('用户名不能为空！');
        }else{
            checkUser($(this).val(),obj_tips);
        }
    });
    //验证密码
    var regexp_pw=/^[a-zA-Z0-9_*@#]{6,18}$/;
    $('input[name="password"]').blur(function(){
        attr_name=$(this).prop('name');
        obj_tips=$('#'+attr_name);
        if($(this).val()==''){
            obj_tips.html('密码不能为空！');
        }else if(!regexp_pw.test($(this).val())){
            obj_tips.html('密码必须由6-18位字母、数字或特殊字符( _ , * , # , @ )组成！');
        }else{
            obj_tips.html('');
        }
    });
    $('input[name="repassword"]').blur(function(){
        attr_name=$(this).prop('name');
        obj_tips=$('#'+attr_name);
        if($(this).val()!==$('input[name="password"]').val()){
            obj_tips.html('确认密码错误！');
        }else{
            obj_tips.html('');
        }
    });
     //验证昵称
    $('input[name="eName"]').blur(function(){
        attr_name=$(this).prop('name');
        obj_tips=$('#'+attr_name);
        if($(this).val()==''){
            obj_tips.html('昵称不能为空！');
        }else{
            obj_tips.html('');
        }
    });

   //验证联系方式
    var regexp_mobile=/^1[3-9]\d{9}$/;
    var regexp_tel=/^0([12]\d{9}|[3-9]\d{9,10})$/;
    $('input[name="tel"]').blur(function(){
        attr_name=$(this).prop('name');
        obj_tips=$('#'+attr_name);
        if($(this).val()==''){
            obj_tips.html('联系方式不能为空！');
        }else if(!regexp_mobile.test($(this).val()) && !regexp_tel.test($(this).val())){
            obj_tips.html('联系方式不合法！');
        }else{
            checkTel($(this).val(),obj_tips);
        }
    });

    var username=password=repassword=eName=tel=role_id='';
    function checkForm(){
        username=$('input[name="username"]').val();
        if(username==''){
            $('#username').html('用户名不能为空！');
            return false;
        }
        password=$('input[name="password"]').val();
        if(password==''){
            $('#password').html('密码不能为空！');
            return false;
        }else if(!regexp_pw.test(password)){
            $('#password').html('密码必须由6-18位字母、数字或特殊字符( _ , * , # , @ )组成！');
            return false;
        }
        repassword=$('input[name="repassword"]').val();
        if(repassword!==password){
            $('#repassword').html('确认密码错误！');
            return false;
        }
        eName=$('input[name="eName"]').val();
        if(eName==""){
            $("#eName").html("昵称不能为空！");
            return false;
        }else{
            $("#eName").html("");
        }
        if($("input:checkbox:checked").length<=0){
            $('#role_id').html('角色至少选择一项！');
            return false;
        }else{
            $('#role_id').html('');
        }
        tel=$('input[name="tel"]').val();
        if(tel==''){
            $('#tel').html('联系方式不能为空！');
            return false;
        }else if(!regexp_mobile.test(tel) && !regexp_tel.test(tel)){
            $('#tel').html('联系方式不合法！');
            return false;
        }
    }

    function postData(){
        var data={username:username,password:password,repassword:repassword,eName:eName,tel:tel};
        $.ajax({
            url:'/Admin/Admin/ajaxCheckData/scene/addAdmin',
            type:'post',
            data:data,
            dataType:'json',
            success:function(re){
                if(re.error==1){
                    $.each(re.errInfo,function(key,val){
                        $('#'+key).html(val);
                    })
                    window.location.href='#mask';
                }else{
                    $('form[name="user"]').prop('action','addbg').prop('method','post');
                    $('form[name="user"]').submit();
                }
            }
        });
    }

    $('.tijiao').click(function(){
        var flag;
        flag=checkForm();//提交前的验证
        if(flag===false){
            window.location.href='#mask';
        }else{
            postData();//发送数据进行验证
        }
    });
</script>